<template>
  <div class="app-container">
    <div class="table-page-search-wrapper">
      <el-form :inline="true" label-width="100px">
        <el-row :gutter="18">
          <el-col :md="8" :sm="24">
            <el-form-item label="物料编号">
              <el-input v-model="queryParam.materialNum" placeholder="" clearable @keyup.enter.native="onSearch" @clear="onSearch" />
            </el-form-item>
          </el-col>
          <el-col :md="8" :sm="24">
            <el-form-item label="物料名称">
              <el-input v-model="queryParam.materialName" placeholder="" clearable @keyup.enter.native="onSearch" @clear="onSearch" />
            </el-form-item>
          </el-col>
          <el-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <el-button v-waves type="primary" icon="el-icon-search" @click="onSearch">
                查询
              </el-button>
              <el-button type="info" icon="el-icon-refresh" @click="reset">
                重置
              </el-button>
            </span>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <div class="table-operator">
      <el-button v-permission="['create']" type="primary" icon="el-icon-plus" @click="$refs.form.open(undefined)">
        新建
      </el-button>
      <el-button v-permission="['import']" type="default" icon="el-icon-upload2" @click="handelImport">
        导入
      </el-button>
      <input
        v-show="false"
        id="fileTemp"
        ref="fileTemp"
        type="file"
        accept=".xls, .xlsx, .excel"
        @input="getImportFile"
      >
      <el-button v-permission="['delete']" icon="el-icon-delete" style="margin-left: 10px;" @click="batchRemove">
        批量删除
      </el-button>
    </div>

    <el-table
      v-loading="loadingData"
      :data="list"
      element-loading-text="Loading"
      fit
      highlight-current-row
      row-key="id"
      @sort-change="appendSorterParam"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column align="center" label="创建时间">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="物料编号">
        <template slot-scope="scope">
          <span>
            {{ scope.row.materialNum }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="物料名称">
        <template slot-scope="scope">
          <span>
            {{ scope.row.materialName }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="规格">
        <template slot-scope="scope">
          <span>
            {{ scope.row.specifications }}
          </span>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" label="物料数量">
        <template slot-scope="scope">
          <span>
            {{ scope.row.materialQuantity }}
          </span>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button
            v-permission="['detail']"
            type="text"
            @click.stop="handleCode(row)"
          >
            二维码
          </el-button>

          <el-button
            v-permission="['detail']"
            type="text"
            @click="$refs.detail.open(row.id)"
          >
            详情
          </el-button>
          <span
            v-permission="['detail']"
            v-permission-again="['update', 'delete']"
          >
            <el-divider
              direction="vertical"
            />
          </span>
          <el-dropdown
            v-permission="['update', 'delete']"
            @command="command => menuCommand(command, row)"
          >
            <el-button type="text">
              更多<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                v-permission="['update']"
                command="update"
                icon="el-icon-edit"
              >
                编辑
              </el-dropdown-item>
              <el-dropdown-item
                v-permission="['delete']"
                command="delete"
                icon="el-icon-delete"
              >
                删除
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="pagination.total>0"
      :total="pagination.total"
      :page.sync="pagination.current"
      :limit.sync="pagination.pageSize"
      :style="{textAlign: 'right'}"
      @pagination="handlePaginationChanged"
    />

    <diboot-form ref="form" @complete="getList" />
    <diboot-detail ref="detail" />
    <!-- 二维码 -->
    <qrCode
      ref="qrCode"
      :label="qrModel.label"
      :text="qrModel.text"
      :qr-size="320"
    />
  </div>
</template>

<script>
import list from '@/components/diboot/mixins/list'
import dibootForm from './form'
import dibootDetail from './detail'
import waves from '@/directive/waves'
import { uploadMaterial } from '@/api/product'
import { columns } from './components/tableConfig'
import qrCode from '@/components/qrCode/index'
import { CodeNo } from '@/unit/QRCodeConfig'

export default {
  name: 'MaterialList',
  directives: { waves },
  components: {
    dibootForm,
    dibootDetail,
    qrCode
  },
  mixins: [list],
  data() {
    return {
      baseApi: '/material',
      getListFromMixin: true,
      columns,
      selectedRowKeys: [],
      qrModel: {
        label: '', // 二维码底部内容
        text: '' // 二维码内容
      },
      CodeNo
    }
  },
  methods: {

    handleSelectionChange(val) {
      this.selectedRowKeys = val.map(item => {
        return item.id
      })
      this.$emit('selectedRowKeys', this.selectedRowKeys)
    },
    // 导入接口
    async setImport(list) {
      const excelData = JSON.stringify(list)
      const res = await uploadMaterial({ excelData })
      if (res.code && res.code !== 200) {
        this.$message.error(res.msg)
        return
      } else {
        this.$message.success(res.data)
      }
      this.getList()
    },

    // 生成二维码
    handleCode(record) {
      // debugger
      const { materialName, materialNum, specifications, id } = record
      const newObj = {
        materialName,
        materialNum,
        id,
        specifications,
        QRCode: CodeNo.Material
      }
      const text = JSON.stringify(newObj)
      this.$refs.qrCode.title = '物料二维码'
      this.qrModel.label = `${materialName} ${materialNum}`
      this.qrModel.text = text
      this.$refs.qrCode.list = this.qrModel.label.split(',')
      this.$refs.qrCode.showModal()
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
